import run from './app/main'
import socket from "./app/socket";
import websocketIo from './app/until/websocket.io'
// 添加服务器websocket 功能
import SocketController from './app/controller/Socket.Controller'
import cluster from 'cluster'
import os from 'os'
const numCpu = os.cpus().length
import port from './config'
import emitEvent from "./app/until/emitEvent";
import {logger} from './app/until/log'

function initSerialport() {
    try {
        let serialName = port.serialName
        let baudRate = parseInt(port.baudRate as any)
        let parity = port.parity
        console.log('========me==========')
        emitEvent.initConfig(port.moduleNum as any)
        SocketController.connectionSerialport(serialName,baudRate as number,parity)
    } catch(err) {
        logger.warn("串口连接参数有问题："+ err)
    }
}
if (cluster.isMaster) {
    websocketIo.getInstance.connection(6677)
    console.log(`Master ${process.pid} is running`);
    console.log("开启主程序")
    initSerialport()
    socket(3456)
    if (numCpu > 1) {
        cluster.fork();
        cluster.fork();
    } else {
        run(6789)
    }
    cluster.on('exit', (worker, code, signal) => {
        console.log(`Worker ${worker.process.pid} died`)
        cluster.fork();
    })
} else {
    run(6789)
    console.log("开启子进程")
}




